<?php

include '../DB/dbConnection.php';

$result=array();

if ($_POST){
	
	$query="SELECT password FROM school WHERE email='".$_POST['email']."'";
		
	$stmt = $conn->prepare($query);
	$success = $stmt->execute();
	$stmt->bind_result($password);

	$count = 0;
	$tmp=array();
	
	while ($stmt->fetch())
	{
		$tmp['password']=$password;
		$count++;
	}

	$stmt->close();
	$conn->close();
	
	if($success && $count==1)
	{
		if(sendMail($_POST['email'],$tmp['password']))
		{
			$result['error']='false';
		}else{
			$result['error']='true';
		}
	}
	else
		$result['error']='true';
}
else
{
	$result['error']='true';
}

echo json_encode($result);

function sendMail($email,$passwd) {	
	$to = $email;
	$from = "Lifeinclass";
	$subject = "Lifeinclass login password";

	$message = "
		<html>
		<head>
		<title>Lifeinclass login password</title>
		</head>
		<body>
		<p>Email : ".$email."</p>
		<p>Password : ".$passwd."</p>
		</body>
		</html>
	";

	// Always set content-type when sending HTML email
	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

	// More headers
	$headers .= 'From: <'.$from.'>' . "\r\n";

	return mail($to,$subject,$message,$headers);
}
?> 
